package com.appspot.androidquote;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;


public class MainActivity extends Activity {

	class GetMessageFromWeb extends AsyncTask<String, Void, ArrayList<String>> {

		@Override
		protected ArrayList<String> doInBackground(String... urls) {
			
			String message = "";
			String author = "";
			ArrayList<String> messageAuthor = new ArrayList<String>(); 
			
	        try {
	        	URL messageUrl = new URL("http://androidquote.appspot.com");
	            BufferedReader in = new BufferedReader(new InputStreamReader(messageUrl.openStream()));
	            message = in.readLine();
	            author = in.readLine();
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
	        messageAuthor.add(message);
	        messageAuthor.add(author);
	        
	        return messageAuthor;
	    }

		@Override
	    protected void onPostExecute(ArrayList<String> result) {
	    	((TextView) findViewById(R.id.tvMessage)).setText(result.get(0));
	    	((TextView) findViewById(R.id.tvAuthor)).setText(result.get(1));
	    }
	}
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.activity_main);
        
        new GetMessageFromWeb().execute("teste");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
